<?php
namespace app\modules\system\actions;

use Yii;
use app\lib\ActionBase;
use app\service\data as data;
use app\lib\Code;

class Manager extends ActionBase {
    
    private $_userdata;
    
    //登录名
    public $username;
    
    //密码
    public $password;
    
    //管理员 id
    public $mid;
    
    //被修改账户 id
    public $bid;
    
    public function __construct() {
        $this->_userdata = new data\SystemData();
    }
    
    public function rules() {
        return [
            //登录
            ['username','required','on'=>'login', 'message'=>'用户名不能为空'],
            ['password','required','on'=>'login', 'message'=>'密码不能为空'],
            
            //删除管理员
            ['mid','required','on'=>'delmanager', 'message'=>'管理员id不能为空'],
            
            //修改管理员密码
            ['mid','required','on'=>'modifymanager', 'message'=>'管理员id不能为空'],
            ['bid','required','on'=>'modifymanager', 'message'=>'/被修改账户id不能为空'],
            ['password','required','on'=>'modifymanager', 'message'=>'密码不能为空'],
        ];
    }
    
    //管理员登录
    public function UserLogin($arrInput) {
        $this->setScenario("login");
        $this->load($arrInput,'');
        if (!$this->validate()) {
            formatJson(Code::PARAM, $this->parseErrors2str($this->firstErrors));
        }
        
        return $this->_userdata->UserLogin($arrInput);
    }
    
    //添加管理员
    public function AddManager($arrInput) {
        $this->setScenario("login");
        $this->load($arrInput,'');
        if (!$this->validate()) {
            formatJson(Code::PARAM, $this->parseErrors2str($this->firstErrors));
        }
        if (! empty($arrInput['password'])) {
            $arrInput['password']=Yii::$app->getSecurity()->generatePasswordHash($arrInput['password']);
        }
        $save = $this->_userdata->AddManager($arrInput);
        if (! $save) {
            formatJson(Code::PARAM, "操作失败");
        }
        return $save;
    }
    
    //删除管理员
    public function DelManager($arrInput) {
        $this->setScenario("delmanager");
        $this->load($arrInput,'');
        if (!$this->validate()) {
            formatJson(Code::PARAM, $this->parseErrors2str($this->firstErrors));
        }
        
        $userdel = $this->_userdata->DelManager($arrInput['mid']);
        if($userdel === -1){
            formatJson(Code::PARAM, "该账户不能删除");
        }
        if (! $userdel) {
            formatJson(Code::PARAM, "操作失败");
        }
        return $userdel;
    }
    
    //修改管理员密码
    public function ModifyManager($arrInput) {
        $this->setScenario("modifymanager");
        $this->load($arrInput,'');
        if (!$this->validate()) {
            formatJson(Code::PARAM, $this->parseErrors2str($this->firstErrors));
        }
        if (! empty($arrInput['password'])) {
            $arrInput['password']=Yii::$app->getSecurity()->generatePasswordHash($arrInput['password']);
        }
        $ret = $this->_userdata->ModifyManager($arrInput);
        if (! $ret) {
            formatJson(Code::PARAM, "操作失败");
        }
        return $ret;
    }
    
    //禁用管理员
    public function CloseManager($arrInput) {
        $this->setScenario("delmanager");
        $this->load($arrInput,'');
        if (!$this->validate()) {
            formatJson(Code::PARAM, $this->parseErrors2str($this->firstErrors));
        }
        
        $userdel = $this->_userdata->CloseManager($arrInput['mid']);
        if($userdel === -1){
            formatJson(Code::PARAM, "该账户不能禁用");
        }
        if (! $userdel) {
            formatJson(Code::PARAM, "操作失败");
        }
        return $userdel;
    }
    
    //禁用管理员
    public function OpenManager($arrInput) {
        $this->setScenario("delmanager");
        $this->load($arrInput,'');
        if (!$this->validate()) {
            formatJson(Code::PARAM, $this->parseErrors2str($this->firstErrors));
        }
        
        $userdel = $this->_userdata->OpenManager($arrInput['mid']);
        if($userdel === -1){
            formatJson(Code::PARAM, "该账户不能操作");
        }
        if (! $userdel) {
            formatJson(Code::PARAM, "操作失败");
        }
        return $userdel;
    }
    
    //管理员列表
    public function ManagerList($arrInput) {
        $session = Yii::$app->session['userInfo'];
        if(!$session) {
            formatJson(Code::PARAM, '请重新登录');
        }
        $arrInput['page'] = $arrInput['page'] ?? 1;
        $arrInput['page_size'] = $arrInput['page_size'] ?? 10;
        return $this->_userdata->ManagerList($arrInput);
    }
    
}